import Vue from 'vue'
import Vuex from 'vuex'
import axios from "@/utils/request" // axios封装引入
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)

const store = new Vuex.Store(
  {
    state: {
      userInfo: {},
      nightMode: true,
      pd_xy: false,
    },

    // 获取属性的状态
    getters: {
      nightMode: state => state.nightMode,
      pd_xy: state => state.pd_xy
    },

    // commit调用
    mutations: {
      userInfo(state, data) {
        window.localStorage.setItem('userInfo', JSON.stringify(data))
        state.userInfo = data
      },
      nightMode(state, data) {
        state.nightMode = data
      },
      pd_xy(state, data) {
        state.pd_xy = data
      }
    },

    // dispath调用mutations中的方法
    actions: {
      getUserInfo(context, num) {
        return new Promise(resolve => {
          axios.get('/user/getUserInfo').then(res => {
            if (res != undefined && res.code == 1) {
              localStorage.setItem('userInfo', JSON.stringify(res.data))
              context.commit('userInfo', res.data)
              resolve(res.data)
            }
          })
        })
      }
    },
    // plugins: [createPersistedState({
    //   storage: localStorage,
    // })]
  }
);

export default store;